package com.sun.tree;

import lombok.AllArgsConstructor;
import lombok.Data;

public class BTree1 {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();

        HerNode node1 = new HerNode(1, "宋江");
        HerNode node2 = new HerNode(2, "吴用");
        HerNode node3 = new HerNode(3, "卢俊义");
        HerNode node4 = new HerNode(4, "林冲");
        node1.setLeft(node2);
        node1.setRight(node3);
        node3.setRight(node4);
        binaryTree.setRoot(node1);
        System.out.println("================前序遍历");
        binaryTree.preOrder();
        System.out.println("================中序遍历");
        binaryTree.infixOrder();
        System.out.println("================后序遍历");
        binaryTree.postOrder();
        System.out.println("================前序遍历查询");
        HerNode herNode = binaryTree.preOrderSearch(3);
        System.out.println(herNode);
    }
}
//定义一个二叉树
class BinaryTree{
    private HerNode root;

    public void setRoot(HerNode root) {
        this.root = root;
    }

    //前序遍历
    public void preOrder(){
        if (this.root != null){
            this.root.preOrder();
        }else {
            System.out.println("当前二叉树为空，不可遍历");
        }
    }
    //中序遍历
    public void infixOrder(){
        if (this.root != null){
            this.root.infixOrder();
        }else {
            System.out.println("当前二叉树为空，不可遍历");
        }
    }
    //后序遍历
    public void postOrder(){
        if (this.root != null){
            this.root.postOrder();
        }else {
            System.out.println("当前二叉树为空，不可遍历");
        }
    }
    //前序遍历查找
    public HerNode preOrderSearch(int no){
        if (this.root != null){
            return this.root.preOrderSearch(no);
        }else {
            return null;
        }
    }
    //中序遍历查找
    public HerNode infixOrderSearch(int no){
        if (this.root != null){
            return this.root.infixOrderSearch(no);
        }else {
            return null;
        }
    }
    //后序遍历查找
    public HerNode postOrderSearch(int no){
        if (this.root != null){
            return this.root.postOrderSearch(no);
        }else {
            return null;
        }
    }
}
class HerNode{
    private int no;
    private String name;
    private HerNode left;
    private HerNode right;
    public HerNode(int no,String name){
        this.no = no;
        this.name = name;
    }

    @Override
    public String toString() {
        return "HerNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HerNode getLeft() {
        return left;
    }

    public void setLeft(HerNode left) {
        this.left = left;
    }

    public HerNode getRight() {
        return right;
    }

    public void setRight(HerNode right) {
        this.right = right;
    }
    //前序
    public void preOrder(){
        System.out.println(this); //先输出父节点
        //递归向左递归
        if (this.left != null){
            this.left.preOrder();
        }
        //递归向右递归
        if (this.right != null){
            this.right.preOrder();
        }
    }
    //中序
    public void infixOrder(){
        //递归向左子树中序遍历
        if (this.left != null){
            this.left.infixOrder();
        }
        //输出父节点
        System.out.println(this);
        if (this.right != null){
            this.right.infixOrder();
        }
    }
    //后序
    public void postOrder(){
        if (this.left != null){
            this.left.postOrder();
        }
        if (this.right != null){
            this.right.postOrder();
        }
        System.out.println(this);
    }
    //前序查找
    public HerNode preOrderSearch(int no){
        //比较当前节点是不是
        if (this.no == no){
            return this;
        }
        HerNode resNode = null;
        if (this.left != null){
            resNode = this.left.preOrderSearch(no);
        }
        if (resNode != null){
            //说明我们左子树找到了节点
            return resNode;
        }
        //
        if (this.right != null){
            resNode = this.right.preOrderSearch(no);
        }
        return resNode;
    }
    //中序遍历
    public HerNode infixOrderSearch(int no ){
        HerNode resNode = null;
        if (this.left != null){
            resNode = this.left.infixOrderSearch(no);
        }
        if (resNode != null){
            return resNode;
        }
        if (this.no == no){
            return this;
        }
        if (this.right != null){
            resNode = this.right.infixOrderSearch(no);
        }
        return resNode;
    }
    //后续遍历
    public HerNode postOrderSearch(int no){
        HerNode resNode = null;
        if (this.left != null){
            resNode = this.left.postOrderSearch(no);
        }
        if (resNode != null){
            return resNode;
        }
        if (this.right != null){
            resNode = this.right.postOrderSearch(no);
        }
        if (resNode != null){
            return resNode;
        }
        if (this.no == no){
            return this;
        }
        return resNode;
    }
}